package com.splic.web.controller.product;

import java.util.List;
import com.splic.common.utils.StringUtils;
import com.splic.product.domain.GoodsQueryDto;
import com.splic.product.domain.GoodsType;
import com.splic.product.domain.Product;
import com.splic.product.dto.GoodsDto;
import com.splic.product.service.GoodsTypeService;
import com.splic.product.service.ProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.splic.common.annotation.Log;
import com.splic.common.enums.BusinessType;
import com.splic.product.domain.Goods;
import com.splic.product.service.GoodsService;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.AjaxResult;
import com.splic.common.utils.poi.ExcelUtil;
import com.splic.common.core.page.TableDataInfo;

/**
 * 订阅套餐Controller
 * 
 * @author singe
 * @date 2022-01-04
 */
@Controller
@RequestMapping("/product/goods")
public class GoodsController extends BaseController
{
    private String prefix = "product/goods";

    @Autowired
    private GoodsService goodsServiceImpl;
    @Autowired
    private GoodsTypeService goodsTypeServiceImpl;

    @Autowired
    private ProductService productServiceImpl;


    @RequiresPermissions("product:goods:view")
    @GetMapping()
    public String goods( ModelMap mmap)
    {
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        return prefix + "/list";
    }

    /**
     * 查询订阅套餐列表
     */
    @RequiresPermissions("product:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsQueryDto goodsQueryDto)
    {
        startPage();
        List<GoodsDto> list = goodsServiceImpl.selectPageList(goodsQueryDto);
        return getDataTable(list);
    }

    /**
     * 根据选择应用查询服务
     */
    @GetMapping("/addChangeProduct")
    @ResponseBody
    public List<GoodsType> addChangeProduct(Long productId)
    {
        GoodsType goodsType = new GoodsType();
        goodsType.setProductId(productId);
        return goodsTypeServiceImpl.selectGoodsTypeList(goodsType);
    }

    /**
     * 新增订阅套餐
     */
    @GetMapping(value = { "/form/{id}", "/form" })
    public String form(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id)){
            Goods goods = goodsServiceImpl.selectGoodsById(id);
            GoodsType type = goodsTypeServiceImpl.selectGoodsTypeById(goods.getGoodsTypeId());
            if ("time".equals(type.getServiceNature())) {
                goods.setCycle1(goods.getCycle());
            }
            mmap.put("goods", goods);
        }else{
            mmap.put("goods", new Goods());
        }

        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        return prefix + "/form";
    }


    /**
     * 新增保存订阅套餐
     */
    @RequiresPermissions("product:goods:edit")
    @Log(title = "订阅套餐", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Goods goods)
    {
        return toAjax(goodsServiceImpl.saveOrUpdate(goods));
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:goods:remove")
    @Log(title = "订阅套餐", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id)
    {
        return toAjax(goodsServiceImpl.deleteById(id));
    }


    /**
     * 导出订阅套餐列表
     */
    @RequiresPermissions("product:goods:export")
    @Log(title = "订阅套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsQueryDto goods)
    {
        List<Goods> list = goodsServiceImpl.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        return util.exportExcel(list, "订阅套餐数据");
    }


    /**
     * 新增订阅套餐
     */
    @GetMapping("/selectGoodsType")
    @ResponseBody
    public List<GoodsType> form(@RequestParam Long productId)
    {
        GoodsType goodsType = new GoodsType();
        goodsType.setProductId(productId);
        List<GoodsType> goodsTypeList = goodsTypeServiceImpl.selectGoodsTypeList(goodsType);
        return goodsTypeList;
    }
}
